#!/bin/python

#
#Xithyl, Test Driven Development for C
#Copyright (C) 2012  Stepan Henek
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from xithyl import logger
from xithyl.Ctypes.basic import *
from xithyl.Ctypes import libc
from xithyl.conditions import basic
from xithyl import actions

logger.addLogger(__name__)

def functional1():
    """
    Create an array where the character is present in the array.
    Use memchar to find the element.
    Check the position.
    """

    result = True

    logger.log("Functional 1", __name__)

    logger.log("Init array", __name__)

    str1 = makeArray(30, 0x0)

    # Test assignment
    result &= basic.arrayEqualsLen([0] * 30, charArrayToList(str1, 30), 30, __name__, True)

    # Set character
    setCharInArray(str1, 10, chr(1))

    # Test the setting
    result &= basic.arrayEqualsLen([0] * 10 + [1] + [0] * 19, charArrayToList(str1, 30), 30, __name__, True)

    # Call memchr
    res = libc.string.memchr(str1, 1, 20)

    # Test the pointer
    result &= basic.numbersEquals(10, distanceOfInstances(str1, res), __name__, True)

    logger.log("Functional 1 done", __name__)

    return result

def functional2():
    """
    Create an array where the character is not present in the array.
    Use memchar to find the element.
    Check the NULL pointer.
    """

    result = True

    logger.log("Functional 2", __name__)

    logger.log("Init array", __name__)

    str1 = makeArray(30, 0x0)

    # Test assignment
    result &= basic.arrayEqualsLen([0] * 30, charArrayToList(str1, 30), 30, __name__, True)

    # Set character
    setCharInArray(str1, 10, chr(3))

    # Test the setting
    result &= basic.arrayEqualsLen([0] * 10 + [3] + [0] * 19, charArrayToList(str1, 30), 30, __name__, True)

    # Call memchr
    res = libc.string.memchr(str1, 1, 20)

    # Test the pointer
    result &= basic.numbersEquals(0, getReferencedAddress(res), __name__, True)

    logger.log("Functional 2 done", __name__)

    return result
